using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GeYiHome.Bussines.BusinesMapping.Adapters;
using GeYiHome.Db.Repository;
using GeYiHome.Domain.Entities;
using GeYiHome.Domain.Specification;
using GeYiHome.Domain.Specification.SpecAbstract;
using GeYiHome.Dto.Request;
using GeYiHome.Dto.Response;
using GeYiHome.PublicLibrary;
using GeYiHome.PublicLibrary.ExceptionEnum;
using GeYiHome.PublicLibrary.Rules;

namespace GeYiHome.Bussines.BusinesService
{
    public class CustomService
    {
        private readonly CustomRepository _customRepository;
        private readonly TypeAdapter _adapter;

        public CustomService()
        {
            _customRepository = new CustomRepository();
            _adapter = Adapter.Mapper;
        }

        #region 添加

        public Guid AddCustom(DtoCustomRequest custom)
        {
            custom.Valid().NotNull(CustomExceptionEnum.ParameterIsNull);
            custom.Valid(x => x.Name).NotNullOrEmpty(CustomExceptionEnum.CustomNameIsNull);
            custom.Valid(x => x.Address).NotNullOrEmpty(CustomExceptionEnum.CustomAddressIsNull);

            var domainData = _adapter.Adapt<DtoCustomRequest, CustomEntity>(custom);

            domainData.Id = Guid.NewGuid();
            domainData.AddUserId = Guid.NewGuid();
            domainData.AddUserName = "wzx";

            _customRepository.Add(domainData);
            _customRepository.UnitOfWork.SaveChanges();
            return domainData.Id;
        }

        #endregion

        #region 修改

        public bool ModifyCustomByLamda(DtoCustomModifyRequest custom)
        {
            custom.Valid().NotNull(CustomExceptionEnum.ParameterIsNull);

            _customRepository.Modify(CustomSpecification.GetById(custom.Id),
                x => new CustomEntity()
                {
                    Name = custom.Name,
                    Gender = custom.Gender,
                    Address = custom.Address,
                    Age = custom.Age,
                    ModifyUserId = Guid.NewGuid(),
                    ModifyUserName = "王忠啸1"
                });
            _customRepository.UnitOfWork.SaveChanges();
            return true;
        }

        public bool ModifyCustomByModel(DtoCustomModifyRequest custom)
        {
            custom.Valid().NotNull(CustomExceptionEnum.ParameterIsNull);

            var entity = _customRepository.Get(custom.Id);
            if (entity == null)
                throw new OpenApiException<CustomExceptionEnum>(CustomExceptionEnum.CustomNotExist);

            entity.Address = custom.Address;
            entity.Age = custom.Age;
            entity.Gender = custom.Gender;
            entity.Name = custom.Name;

            _customRepository.Modify(entity);
            _customRepository.UnitOfWork.SaveChanges();
            return true;
        }

        #endregion

        #region 删除

        public bool DeleteByModel(Guid id)
        {
            var entity = _customRepository.Get(id);
            if (entity == null)
                throw new OpenApiException<CustomExceptionEnum>(CustomExceptionEnum.CustomNotExist);
            _customRepository.Remove(entity);
            _customRepository.UnitOfWork.SaveChanges();
            return true;
        }

        public bool DeleteBySpecification(int minAge, int maxAge)
        {
            _customRepository.Remove(CustomSpecification.GetRangeByAge(minAge, maxAge));
            _customRepository.UnitOfWork.SaveChanges();
            return true;
        }

        public bool DeleteById(Guid id)
        {
            _customRepository.Remove(id);
            _customRepository.UnitOfWork.SaveChanges();
            return true;
        }

        #endregion

        #region 查询

        public DtoCustomResponse GetCustomById(Guid id)
        {
            var entity = _customRepository.Get(id);
            var mapperData = _adapter.Adapt<CustomEntity, DtoCustomResponse>(entity);
            return mapperData;
        }

        public List<DtoCustomResponse> GetList(string name, DateTime? dateTime)
        {
            var entities =
                _customRepository.GetAllMatching(CustomSpecification.GetByNameAndAddTime(name, dateTime)).ToList();
            var mapperDatas = _adapter.Adapt<List<CustomEntity>, List<DtoCustomResponse>>(entities);
            return mapperDatas;
        }

        public DataPageList<DtoCustomResponse> GetPageList(int pageIndex, int pageSize, string name, DateTime? dateTime)
        {
            var entities = _customRepository.GetPageList(pageIndex, pageSize,
                CustomSpecification.GetByNameAndAddTime(name, dateTime), x => x.AddTime, SortEnum.Desc);
            var mapperDatas = _adapter.Adapt<DataPageList<CustomEntity>, DataPageList<DtoCustomResponse>>(entities);
            return mapperDatas;
        }

        #endregion
    }
}
